﻿package com.duartepeixinho
{
	import com.duartepeixinho.experimentLabel.Label;
	import com.duartepeixinho.fps.FpsCounter;
	import com.duartepeixinho.particles.animationParticles;
	import com.duartepeixinho.reflection.reflection;
	import com.duartepeixinho.tree.Tree;
	import flash.events.MouseEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	
	public class treeParticle extends Sprite 
	{
		
		private var _tree:Tree;
		//var to control the animation
		private var _animation:Boolean = false;
		private var _pixelize:animationParticles;
		private var _reflection:reflection = new reflection();

		public function treeParticle():void 
		{
			_tree = new Tree(300);
			_tree.x = stage.stageWidth / 2;
			_tree.y = 100;
			addChild(_tree);												
			addEventListener(Event.ENTER_FRAME, loop);
			addChild(_reflection);
			var fps:FpsCounter = new FpsCounter(stage);
			fps.y = 25;
			addChild(fps);
			addChild(new Label(stage, "Exploding Tree", "<a href=\"http://www.duartepeixinho.com\">DuartePeixinho.com</a>"));
			stage.addEventListener(MouseEvent.CLICK, clicked);
		}
		private function clicked(e:MouseEvent):void {
			if (_animation && _pixelize.getAnimationStatus()) {
				_pixelize.alpha = 1;			
				_tree = new Tree(300);
				_tree.x = stage.stageWidth / 2;
				_tree.y = 100;
				removeChild(_pixelize);
				addChild(_tree);
				_animation = false;
			}
		}
		private function loop(e:Event):void {
			_reflection.createReflection(stage, stage.stageWidth, stage.stageHeight, [0, 127], 1, 0xffffff, null, false );
			_reflection.y = 230;
			_reflection.x = 0;
			//only start the particle when the tree is built
			if (!_animation && _tree.getTreeStatus()) {				
				_animation = true;
				_pixelize = new animationParticles(300, _tree, 4, 4);							
				addChild(_pixelize);
				removeChild(_tree);
				_pixelize.x = _tree.x
				_pixelize.y = _tree.y
				_pixelize.startAnimation();
				_pixelize.filters = [new BlurFilter(2,2,2)];
			}
			//when _animation is running and _pixelize.getAnimationStatus is finished

			else if (_animation) {
				if (_pixelize.getAnimationStatus() && _pixelize.alpha>0) _pixelize.alpha -= 0.01;
			}
		}
		
	}
	
}